package com.kly.sc.api.application.service.user.impl;

import cn.hutool.json.JSONObject;
import com.kly.sc.api.application.vo.BenefitDto;
import com.kly.sc.api.application.vo.ChatDataDto;
import com.kly.user.enums.ConversationType;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class ChatSumHelper {

    /**
     * 总共发送的消息条数
     */
    private Integer totalChatCount;
    /**
     * 是否发送过消息
     */
    private boolean hasChatBefore = false;
    /**
     * 当前会话阶段-破冰会话
     */
    private Integer iceStageCount = 0;
    /**
     * 当前会话阶段-有效会话
     */
    private Integer validStageCount = 0;
    /**
     * 当前会话阶段-深度会话
     */
    private Integer deepStageCount = 0;
    /**
     * 是否达到破冰会话限制
     */
    private boolean reachIceLimit = false;
    /**
     * 是否达到有效会话限制
     */
    private boolean reachValidLimit = false;
    /**
     * 是否达到深度会话限制
     */
    private boolean reachDeepLimit = false;
    /**
     * 是否达到破冰会话限制
     */
    private boolean reachMaxNumLimit = false;
    /**
     * 当前会话中已发送消息条数
     */
    private Integer sendCount = 0;
    /**
     * 剩余可用的发送消息数
     */
    private int remainCount = 0;


    private ChatDataDto matchDto;


    /**
     * chatDataDtoList：当日消息发送情况
     * totalMsgNum: 当前会话消息发送总数
     */
    public ChatSumHelper(List<ChatDataDto> chatDataDtoList, Integer totalMsgNum, String toUserCode, BenefitDto benefit) {

        chatDataDtoList.forEach(chatDataDto -> {
            if (ConversationType.FIRST_CONVERSATION.equals(chatDataDto.getConversationType())) {
                iceStageCount++;
            } else if (ConversationType.EFFECTIVE_CONVERSATIONS.equals(chatDataDto.getConversationType())) {
                validStageCount++;
                iceStageCount++;
            } else if (ConversationType.DEEP_CONVERSATIONS.equals(chatDataDto.getConversationType())) {
                deepStageCount++;
                iceStageCount++;
                validStageCount++;
            }

            if (chatDataDto.getToUserCode().equals(toUserCode)) {
                sendCount = chatDataDto.getHasSendMsgNum();
                hasChatBefore = true;
                matchDto = chatDataDto;
                if (totalMsgNum >= benefit.getOverLimitChat()) {
                    reachMaxNumLimit = true;
                }
            }
        });


        if (iceStageCount >= benefit.getFirstChatLimitNum()) {
            reachIceLimit = true;
        }
        if (validStageCount >= benefit.getEffectiveChatLimitNum()) {
            reachValidLimit = true;
        }

        if (deepStageCount >= benefit.getDeepChatLimitNum()) {
            reachDeepLimit = true;
        }
    }


    public int getRemainCount(BenefitDto benefit) {
        // 当前会话非新开会话
        if (hasChatBefore) {

            // 未发送过消息或在破冰会话之前
            if (sendCount <=1) {
                if(reachIceLimit){
                    return ConversationType.FIRST_CONVERSATION.getSize() - sendCount;
                }
                if (reachValidLimit) {
                    return ConversationType.EFFECTIVE_CONVERSATIONS.getSize() - sendCount;
                }
                if (reachDeepLimit) {
                    return ConversationType.DEEP_CONVERSATIONS.getSize() - sendCount;
                }
                return benefit.getOverLimitChat() - sendCount;
            }
            // 处于破冰会话阶段
            else if (ConversationType.FIRST_CONVERSATION.equals(matchDto.getConversationType())) {
                if (reachValidLimit) {
                    return ConversationType.EFFECTIVE_CONVERSATIONS.getSize() - sendCount;
                }
                if (reachDeepLimit) {
                    return ConversationType.DEEP_CONVERSATIONS.getSize() - sendCount;
                }
                return benefit.getOverLimitChat() - sendCount;
            }
            // 处于有效会话阶段
            else if (ConversationType.EFFECTIVE_CONVERSATIONS.equals(matchDto.getConversationType())) {

                if (reachDeepLimit) {
                    return ConversationType.DEEP_CONVERSATIONS.getSize() - sendCount;
                }
                return benefit.getOverLimitChat() - sendCount;
            }
            // 处于深度会话
            else if (ConversationType.DEEP_CONVERSATIONS.equals(matchDto.getConversationType())) {
                return benefit.getOverLimitChat() - sendCount;
            }
        } else {
            if (reachIceLimit) {
                return ConversationType.FIRST_CONVERSATION.getSize()-sendCount;
            }
            if (reachValidLimit) {
                return ConversationType.EFFECTIVE_CONVERSATIONS.getSize() - sendCount;
            }
            if (reachDeepLimit) {
                return ConversationType.DEEP_CONVERSATIONS.getSize() - sendCount;
            }

            return benefit.getOverLimitChat() - sendCount;
        }


        return benefit.getOverLimitChat() - sendCount;
    }

    public Integer getTotalChatCount() {
        return totalChatCount;
    }

    public void setTotalChatCount(Integer totalChatCount) {
        this.totalChatCount = totalChatCount;
    }

    public boolean isHasChatBefore() {
        return hasChatBefore;
    }

    public void setHasChatBefore(boolean hasChatBefore) {
        this.hasChatBefore = hasChatBefore;
    }

    public Integer getIceStageCount() {
        return iceStageCount;
    }

    public void setIceStageCount(Integer iceStageCount) {
        this.iceStageCount = iceStageCount;
    }

    public Integer getValidStageCount() {
        return validStageCount;
    }

    public void setValidStageCount(Integer validStageCount) {
        this.validStageCount = validStageCount;
    }

    public Integer getDeepStageCount() {
        return deepStageCount;
    }

    public void setDeepStageCount(Integer deepStageCount) {
        this.deepStageCount = deepStageCount;
    }

    public boolean isReachIceLimit() {
        return reachIceLimit;
    }

    public void setReachIceLimit(boolean reachIceLimit) {
        this.reachIceLimit = reachIceLimit;
    }

    public boolean isReachValidLimit() {
        return reachValidLimit;
    }

    public void setReachValidLimit(boolean reachValidLimit) {
        this.reachValidLimit = reachValidLimit;
    }

    public boolean isReachDeepLimit() {
        return reachDeepLimit;
    }

    public void setReachDeepLimit(boolean reachDeepLimit) {
        this.reachDeepLimit = reachDeepLimit;
    }

    public boolean isReachMaxNumLimit() {
        return reachMaxNumLimit;
    }

    public void setReachMaxNumLimit(boolean reachMaxNumLimit) {
        this.reachMaxNumLimit = reachMaxNumLimit;
    }

    public Integer getSendCount() {
        return sendCount;
    }

    public void setSendCount(Integer sendCount) {
        this.sendCount = sendCount;
    }

    public void setRemainCount(int remainCount) {
        this.remainCount = remainCount;
    }

    public ChatDataDto getMatchDto() {
        return matchDto;
    }

    public void setMatchDto(ChatDataDto matchDto) {
        this.matchDto = matchDto;
    }
}
